//
//  CGImageCreateWithImageInRectViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/11/8.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CGImageCreateWithImageInRectViewController.h"

@interface CGImageCreateWithImageInRectViewController ()

@property (nonatomic, strong) UIImageView *imageV;

@end

@implementation CGImageCreateWithImageInRectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createImage];
}

- (void)createImage
{
    UIImageView *tempImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    tempImageV.image = [UIImage imageWithName:@"snowMan"];
    UIGraphicsBeginImageContext(tempImageV.frame.size);
    CGContextRef ref = UIGraphicsGetCurrentContext();
    [tempImageV.layer renderInContext:ref];
    CGImageRef currentRef = UIGraphicsGetImageFromCurrentImageContext().CGImage;
    UIGraphicsEndImageContext();
    
    CGImageRef imageRef = CGImageCreateWithImageInRect(currentRef, CGRectMake(100, 100, 100, 100)); // 区域剪切
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    self.imageV.image = [UIImage imageWithCGImage:imageRef];
    [self.view addSubview:_imageV];
}


@end
